package com.ycsj.goldmedalnewconcept.activity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.ycsj.goldmedalnewconcept.bean.TeacherFansiResponse;
import com.ycsj.goldmedalnewconcept.bean.TeacherFansiResponse.FansiContent;
import com.ycsj.goldmedalnewconcept.constant.Constant;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.R;

//查看教师的反思
public class CheckFansiActivity extends Activity implements OnClickListener {
	
	private ImageView iv_back;
	private TextView tv_content;
	private String user_id;
	private int unit;
	private int lesson;
	private String class_id;
	private String account;
	private String rowid;
	private List<FansiContent> list;
	private int y = -1;
	
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_checkfansi);
		user_id = getIntent().getStringExtra("user_id");
		unit = getIntent().getIntExtra("unit", -1);
		lesson = getIntent().getIntExtra("Lesson", -1);
		class_id = getIntent().getStringExtra("class_id");
		account = SPUtil.getString(this, "account", "");
//		intent2.putExtra("class_id", class_id);
//		intent2.putExtra("user_id",user_id);
//		intent2.putExtra("Lesson",Lesson);
//		intent2.putExtra("unit", unit);
		list = new ArrayList<FansiContent>();
		initView();
		setOnListener();
		initData();
	}

	private void initView() {
		// TODO Auto-generated method stub
		iv_back = (ImageView) findViewById(R.id.iv_back);
		tv_content = (TextView) findViewById(R.id.tv_content);
	}

	private void initData() {
		OkHttpClient mOkHttpClient = new OkHttpClient();
		// mOkHttpClient.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
		FormEncodingBuilder builder = new FormEncodingBuilder();
		builder.add("type", "22");
		builder.add("c", user_id);
		builder.add("role", "2");
		builder.add("schoolid", "");
		builder.add("userclass",class_id);
		// http://192.168.0.198:8888/ycsj_platform/ManageMsg?type=22&c=13006314760&role=2&userclass=1253
		Request request = new Request.Builder()
				.url(Constant.SERVER_LOC + "ManageMsg").post(builder.build())
				.build();
		mOkHttpClient.newCall(request).enqueue(new Callback() {

			@Override
			public void onFailure(Request arg0, IOException arg1) {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						Toast.makeText(getBaseContext(), "网络无响应，请检查网络连接", 0)
								.show();
					}
				});
			}

			@Override
			public void onResponse(Response arg0) throws IOException {
				// 解析Json数据，保存登录状态及身份信息

				Gson gson = new Gson();

				String json = arg0.body().string();
				TeacherFansiResponse fromJson = gson.fromJson(json,
						TeacherFansiResponse.class);
				Log.e("test", json);
				if (fromJson != null) {
					if (!"-1".equals(fromJson.state)) {

						list = fromJson.list;
						String u = unit + "";
						String l = lesson + "";

						for (int i = 0; i < list.size(); i++) {

							if (u.equals(list.get(i).unit)
									&& l.equals(list.get(i).lesson)) {
								y = i;
							}
						}
						
						if (y != -1) {
							rowid = list.get(y).row_id;
							runOnUiThread(new Runnable() {
								@Override
								public void run() {
									tv_content.setText(list.get(y).content);
									init();
								}

								private void init() {
									OkHttpClient mOkHttpClient = new OkHttpClient();
									// mOkHttpClient.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
									FormEncodingBuilder builder = new FormEncodingBuilder();
									builder.add("type", "21");
									builder.add("rowid", rowid);
									builder.add("c", account);
									builder.add("role", "3");
									builder.add("schoolid","");
									builder.add("userclass", class_id);
									// http://192.168.0.198:8888/ycsj_platform/ManageMsg?type=22&c=13006314760&role=2&userclass=1253
									Request request = new Request.Builder()
											.url(Constant.SERVER_LOC + "ManageMsg").post(builder.build())
											.build();
									mOkHttpClient.newCall(request).enqueue(new Callback() {

										@Override
										public void onFailure(Request arg0, IOException arg1) {
											runOnUiThread(new Runnable() {
												@Override
												public void run() {
													Toast.makeText(getBaseContext(), "网络无响应，请检查网络连接", 0)
															.show();
												}
											});
										}

										@Override
										public void onResponse(Response arg0) throws IOException {
											// 解析Json数据，保存登录状态及身份信息

											Gson gson = new Gson();

											String json = arg0.body().string();
											TeacherFansiResponse fromJson = gson.fromJson(json,
													TeacherFansiResponse.class);
											Log.e("test", json+"888");
											
										}
									});
								}
							});
						}
					}
				}
			}
		});
	}

	private void setOnListener() {
		// TODO Auto-generated method stub
		iv_back.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.iv_back:
			finish();
			break;
		}
	}
}














